package com.sip.stream.utils;

import android.media.MediaCodec;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.util.Log;

import com.serenegiant.usb.UVCCamera;
import com.sip.stream.utils.zfy.ZFYMediaUtil;
import com.sip.stream.utils.zfy.config.ZFYMediaConfig;

import java.util.ArrayList;


/**
 * H264 H265视频编码器工具类
 */
public class GBUtils {

    private static final String TAG = "GBUtils";

    public static String[] getVideoMinetype() {
        return new String[]{"H.264/AVC", "H.265/HEVC"};
    }

    public static int[] getPPISelector() {
        return new int[]{1080,720};
    }

    /***
     * 创建编码器
     */
    public static MediaCodec createMediaCodec(int width, int height, int bitrate, int frameRate, int iFrameInterval, String streamMineType, String messageTag) {
        MediaCodec mediaCodec;
        if (streamMineType.equalsIgnoreCase("video/hevc"))
        {
            MediaFormat hEVCMediaFormat = getHEVCMediaFormat(width, height, bitrate, frameRate, iFrameInterval);
            if (new MediaCodecList(1).findEncoderForFormat(hEVCMediaFormat) == null) {
                Log.e(TAG, "setUpMediacodec failed: findEncoderForFormat null");
            }
            try {
                mediaCodec = MediaCodec.createEncoderByType("video/hevc");
                try {
                    Log.i(messageTag, "mediaCodec name " + mediaCodec.getName());
                } catch (Exception e) {
                    Log.i(TAG, "createEncoderByType" + e.getLocalizedMessage());
                    e.printStackTrace();
                    mediaCodec.reset();
                    mediaCodec.configure(hEVCMediaFormat,null,  null, 1);
                    return mediaCodec;
                }
            } catch (Exception e) {
                mediaCodec = null;
                e.printStackTrace();
            }
            try {
                mediaCodec.reset();
                mediaCodec.configure(hEVCMediaFormat, null,  null, 1);
            } catch (Exception e) {
                Log.i(TAG, "configure" + e.getLocalizedMessage());
                e.printStackTrace();
            }
        } else {
            mediaCodec = null;
        }

        if (mediaCodec == null)
        {
            MediaFormat aVCMediaFormat = getAVCMediaFormat(width, height, bitrate, frameRate, iFrameInterval);
            if (new MediaCodecList(1).findEncoderForFormat(aVCMediaFormat) == null) {
                Log.e(TAG, "setUpMediacodec failed: findEncoderForFormat null");
            }
            try {
                mediaCodec = MediaCodec.createEncoderByType("video/avc");
                Log.i(messageTag, "mediaCodec name " + mediaCodec.getName());
            } catch (Exception e) {
                Log.i(TAG, "createEncoderByType" + e.getLocalizedMessage());
                e.printStackTrace();
            }
            try {
                mediaCodec.reset();
                mediaCodec.configure(aVCMediaFormat,null,null, 1);
            } catch (Exception e) {
                Log.i(TAG, "configure" + e.getLocalizedMessage());
                e.printStackTrace();
            }
        }
        return mediaCodec;
    }

    /**
     * H264 编码器
     * @param width
     * @param height
     * @param bitrate
     * @param frameRate
     * @param iFrameInterval
     * @return
     */
    public static MediaFormat getAVCMediaFormat(int width, int height, int bitrate, int frameRate, int iFrameInterval) {
        MediaFormat createVideoFormat = MediaFormat.createVideoFormat("video/avc", width, height);
        createVideoFormat.setInteger("color-format", 21);
        createVideoFormat.setInteger("bitrate", bitrate);
        createVideoFormat.setInteger("frame-rate", frameRate);
        createVideoFormat.setInteger("i-frame-interval", iFrameInterval);
        return createVideoFormat;
    }


    /**
     * H265 编码器
     * @param width
     * @param height
     * @param bitrate
     * @param frameRate
     * @param iFrameInterval
     * @return
     */
    public static MediaFormat getHEVCMediaFormat(int width, int height, int bitrate, int frameRate, int iFrameInterval) {
        MediaFormat createVideoFormat = MediaFormat.createVideoFormat("video/hevc", width, height);
        createVideoFormat.setInteger("color-format", 21);
        createVideoFormat.setInteger("bitrate", bitrate);
        createVideoFormat.setInteger("frame-rate", frameRate);
        createVideoFormat.setInteger("i-frame-interval", iFrameInterval);
        return createVideoFormat;
    }


    /**
     * 设置传输流的PPI
     * @param streamPPI
     * @param zFYMediaConfig
     */
    public static void setPreViewStreamPPI(String streamPPI, ZFYMediaConfig zFYMediaConfig) {
        Log.i(TAG, "setStreamPPI " + streamPPI);
        if (!zFYMediaConfig.isDualStream()) {
            setPreViewVideoPPI(streamPPI, zFYMediaConfig);
            return;
        }
        String[] split = streamPPI.split(ZFYMediaUtil.SEP_PPI);
        if (split.length == 2) {
            int parseInt = Integer.parseInt(split[0]);
            int parseInt2 = Integer.parseInt(split[1]);
            zFYMediaConfig.setStreamHeight(parseInt2);
            zFYMediaConfig.setStreamWidth(parseInt);
            Log.i(TAG, "width " + parseInt + " height " + parseInt2);
            zFYMediaConfig.setVideoHeight(parseInt2);
            zFYMediaConfig.setVideoWidth(parseInt);
        }
    }


    private static void setPreViewVideoPPI(String str, ZFYMediaConfig zFYMediaConfig) {
        String[] split = str.split(ZFYMediaUtil.SEP_PPI);
        if (split.length == 2) {
            int parseInt = Integer.parseInt(split[0]);
            int parseInt2 = Integer.parseInt(split[1]);
            zFYMediaConfig.setVideoHeight(parseInt2);
            zFYMediaConfig.setVideoWidth(parseInt);
            zFYMediaConfig.setStreamHeight(parseInt2);
            zFYMediaConfig.setStreamWidth(parseInt);
        }
    }
}
